<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class vt_model extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    /*
    function getAllNguoiBan(){
        $this->db->select('*');
        $this->db->from('bql_nguoiban');
        $query = $this->db->get();
        
        if($query->num_rows() > 0){
            return $query->result();
        }
        return false;
    }
    
    function getNBById($id){
        $this->db->select('nb.*');
        $this->db->from('bql_nguoiban As nb');
        $this->db->select('kv.tenkhu');
        $this->db->join('bql_khuvuc As kv', 'kv.id = nb.makhuvuc');
        $this->db->where('nb.id',$id);
        $query = $this->db->get();
        
        if($query->num_rows() > 0){
            return $query->row();
        }
        return false;
    }
    
    function getKhuVucById($id){
        $this->db->select('*');
        $this->db->from('bql_khuvuc');
        $this->db->where('id',$id);
        $query = $this->db->get();
        
        if($query->num_rows() > 0){
            return $query->row();
        }
        return false;
    }
    
    function getALLKhuVuc(){
        $this->db->select('*');
        $this->db->from('bql_khuvuc');
        $query = $this->db->get();
        
        if($query->num_rows() > 0){
            return $query->result();
        }
        return false;
    }
    
    
    
    
    
    function deleteNB($id){
        $this->db->delete('bql_nguoiban', array('id' => $id));
    }
    */
    
    /*
    * Don vi tinh
    */
    function getAllDVT(){
        $query = $this->db->get('khvt_unit');
        if($query->num_rows() > 0){
            return $query->result();
        }
        return false;
    }
    function getValueById($id){
        $query = $this->db->get_where('khvt_unit', array('id' => $id));
        if($query->num_rows() == 1){
            return $query->row();
        }
        return false;
    }
    function insertDVT($data){
        $this->db->insert('khvt_unit', $data);
        return $this->db->insert_id();
    }
    function updateDVT($data,$id){
        $this->db->where('id', $id);
        $this->db->update('khvt_unit', $data);
    }
    
    function load_datatable_dvt($GET,$step = 1){
        
        $aColumns = array( 'dvt.id', 'dvt.name', 'dvt.status' );
        
        $this->db->select('dvt.*');
        $this->db->from('khvt_unit As dvt');
        $this->db->where('dvt.status !=',-1);
    
        /*
         * Ordering
         */
        if ( isset( $GET['iSortCol_0'] ) )
        {
            for ( $i=0 ; $i<intval( $GET['iSortingCols'] ) ; $i++ )
            {
                if ( $GET[ 'bSortable_'.intval($GET['iSortCol_'.$i]) ] == "true" )
                {
                    if( $aColumns[ $GET['iSortCol_'.$i] ] != ''){
                        $this->db->order_by($aColumns[ $GET['iSortCol_'.$i] ], $GET['sSortDir_'.$i] );
                    }
                }
            }
        }

        /* 
         * Filtering
         */
        if ( isset($GET['sSearch']) && $GET['sSearch'] != "" )
        {   
            if($aColumns[1] != ''){
                $this->db->like($aColumns[1], $GET['sSearch'] );    
                $this->db->or_like($aColumns[0], $GET['sSearch'] );    
            }
        }
    
        /* Individual column filtering */
        for ( $i=0 ; $i<count($aColumns) ; $i++ )
        {
            if ( isset($GET['bSearchable_'.$i]) && $GET['bSearchable_'.$i] == "true" && $GET['sSearch_'.$i] != '' )
            {
                if($aColumns[$i] != ''){
                    $this->db->like($aColumns[$i], $GET['sSearch_'.$i] );
                }
            }
        }
        
        $total = 0;
        $aaData = array();
        
        switch($step){
            case 1:
                /* 
                 * Paging
                 */
                if ( isset( $GET['iDisplayStart'] ) && $GET['iDisplayLength'] != '-1' ){
                    $this->db->limit($GET['iDisplayLength'], $GET['iDisplayStart']);
                }
                
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
                
                if($total > 0){
                    foreach($query->result() As $item){
                        $html = '';
                        if($item->status == -1){
                            $html = '<img src="'.base_url('template/ess/images/trash.png').'" alt="">';
                        }elseif($item->status == 1){
                            $html = '<img src="'.base_url('template/ess/images/tick.png').'" alt="">';
                        }elseif($item->status == 2){
                            $html = '<img src="'.base_url('template/ess/images/publish_x.png').'" alt="">';
                        }
                        
                        $row = array();
                        //$row[] = '<input type="radio" name="tableselected" value="">';
                        $row[] = $item->id;
                        $row[] = $item->name;
                        $row[] = $html;
                        $row['id'] = $item->id;
                        $row['modify'] = 'edit';
                        $aaData[] = $row;
                    }
                }    
            
            break;
            
            case 2:
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
            break;
            
            default:
            
            break;
        }
        
        $output = array(
            "total" => $total,
            "aaData" => $aaData
        );
    
        return $output;
        
        
    }

    function load_datatable_pl($GET,$step = 1){
        
        $aColumns = array( 'ctg.id', 'ctg.name', 'ctg.status' );
        
        $this->db->select('ctg.*');
        $this->db->from('khvt_category As ctg');
        $this->db->where('ctg.status !=',-1);
    
        /*
         * Ordering
         */
        if ( isset( $GET['iSortCol_0'] ) )
        {
            for ( $i=0 ; $i<intval( $GET['iSortingCols'] ) ; $i++ )
            {
                if ( $GET[ 'bSortable_'.intval($GET['iSortCol_'.$i]) ] == "true" )
                {
                    if( $aColumns[ $GET['iSortCol_'.$i] ] != ''){
                        $this->db->order_by($aColumns[ $GET['iSortCol_'.$i] ], $GET['sSortDir_'.$i] );
                    }
                }
            }
        }

        /* 
         * Filtering
         */
        if ( isset($GET['sSearch']) && $GET['sSearch'] != "" )
        {   
            if($aColumns[1] != ''){
                $this->db->like($aColumns[1], $GET['sSearch'] );    
                $this->db->or_like($aColumns[0], $GET['sSearch'] );    
            }
        }
    
        /* Individual column filtering */
        for ( $i=0 ; $i<count($aColumns) ; $i++ )
        {
            if ( isset($GET['bSearchable_'.$i]) && $GET['bSearchable_'.$i] == "true" && $GET['sSearch_'.$i] != '' )
            {
                if($aColumns[$i] != ''){
                    $this->db->like($aColumns[$i], $GET['sSearch_'.$i] );
                }
            }
        }
        
        $total = 0;
        $aaData = array();
        
        switch($step){
            case 1:
                /* 
                 * Paging
                 */
                if ( isset( $GET['iDisplayStart'] ) && $GET['iDisplayLength'] != '-1' ){
                    $this->db->limit($GET['iDisplayLength'], $GET['iDisplayStart']);
                }
                
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
                
                if($total > 0){
                    foreach($query->result() As $item){
                        $html = '';
                        if($item->status == -1){
                            $html = '<img src="'.base_url('template/ess/images/trash.png').'" alt="">';
                        }elseif($item->status == 1){
                            $html = '<img src="'.base_url('template/ess/images/tick.png').'" alt="">';
                        }elseif($item->status == 2){
                            $html = '<img src="'.base_url('template/ess/images/publish_x.png').'" alt="">';
                        }
                        
                        $row = array();
                        //$row[] = '<input type="radio" name="tableselected" value="">';
                        $row[] = $item->id;
                        $row[] = $item->name;
                        $row[] = $html;
                        $row['id'] = $item->id;
                        $row['modify'] = 'edit';
                        $aaData[] = $row;
                    }
                }    
            
            break;
            
            case 2:
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
            break;
            
            default:
            
            break;
        }
        
        $output = array(
            "total" => $total,
            "aaData" => $aaData
        );
    
        return $output;        
    }

    function getAllCategory(){
        $query = $this->db->get('khvt_category');
        if($query->num_rows() > 0){
            return $query->result();
        }
        return false;
    }
    function getCatgById($id){
        $query = $this->db->get_where('khvt_category', array('id' => $id));
        if($query->num_rows() == 1){
            return $query->row();
        }
        return false;
    }
    function insertPL($data){
        $this->db->insert('khvt_category', $data);
        return $this->db->insert_id();
    }
    function updatePL($data,$id){
        $this->db->where('id', $id);
        $this->db->update('khvt_category', $data);
    }
    
    function load_datatable_vt($GET,$step = 1){
        
        $aColumns = array( 'pro.id', 'pro.code', 'pro.name', 'val.name', 'pro.total', 'cat.name','pro.price','','pro.status' );
        
        $this->db->select('pro.*');
        $this->db->from('khvt_product As pro');
        $this->db->select('cat.name As catg_name');
        $this->db->join('khvt_category As cat', 'cat.id = pro.catg_id');
        $this->db->select('val.name As val_name');
        $this->db->join('khvt_unit As val', 'val.id = pro.unit_id');
        $this->db->where('pro.status !=',-1);
        
        if( isset($GET['filter_category']) && $GET['filter_category'] != 0 ){
            $this->db->where('pro.catg_id',$GET['filter_category']);
        }
        
        /*
         * Ordering
         */
        if ( isset( $GET['iSortCol_0'] ) )
        {
            for ( $i=0 ; $i<intval( $GET['iSortingCols'] ) ; $i++ )
            {
                if ( $GET[ 'bSortable_'.intval($GET['iSortCol_'.$i]) ] == "true" )
                {
                    if( $aColumns[ $GET['iSortCol_'.$i] ] != ''){
                        $this->db->order_by($aColumns[ $GET['iSortCol_'.$i] ], $GET['sSortDir_'.$i] );
                    }
                }
            }
        }

        /* 
         * Filtering
         */
        if ( isset($GET['sSearch']) && $GET['sSearch'] != "" )
        {   
            $this->db->like($aColumns[1], $GET['sSearch'] );    
            $this->db->or_like($aColumns[2], $GET['sSearch'] );    
        }
    
        /* Individual column filtering */
        for ( $i=0 ; $i<count($aColumns) ; $i++ )
        {
            if ( isset($GET['bSearchable_'.$i]) && $GET['bSearchable_'.$i] == "true" && $GET['sSearch_'.$i] != '' )
            {
                if($aColumns[$i] != ''){
                    $this->db->like($aColumns[$i], $GET['sSearch_'.$i] );
                }
            }
        }
        
        $total = 0;
        $aaData = array();
        
        switch($step){
            case 1:
                /* 
                 * Paging
                 */
                if ( isset( $GET['iDisplayStart'] ) && $GET['iDisplayLength'] != '-1' ){
                    $this->db->limit($GET['iDisplayLength'], $GET['iDisplayStart']);
                }
                
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
                
                if($total > 0){
                    foreach($query->result() As $item){
                        $html = '';
                        if($item->status == -1){
                            $html = '<img src="'.base_url('template/ess/images/trash.png').'" alt="">';
                        }elseif($item->status == 1){
                            $html = '<img src="'.base_url('template/ess/images/tick.png').'" alt="">';
                        }elseif($item->status == 2){
                            $html = '<img src="'.base_url('template/ess/images/publish_x.png').'" alt="">';
                        }
                        
                        $row = array();
                        //$row[] = '<input type="radio" name="tableselected" value="">';
                        $row[] = $item->id;
                        $row[] = $item->code;
                        $row[] = $item->name;
                        $row[] = $item->val_name;
                        $row[] = $item->catg_name;
                        $row[] = decimal_format($item->total);
                        $row[] = formatMoney(decimal_format($item->price));
                        $row[] = formatMoney(decimal_format($item->price * $item->total));
                        $row[] = $html;
                        $row['id'] = $item->id;
                        $row['modify'] = 'edit';
                        $aaData[] = $row;
                    }
                }    
            
            break;
            
            case 2:
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
            break;
            
            default:
            
            break;
        }
        
        $output = array(
            "total" => $total,
            "aaData" => $aaData
        );
    
        return $output;
        
        
    }
    
    function getVTById($id){
        $this->db->select('pro.*');
        $this->db->from('khvt_product As pro');
        $this->db->select('cat.name As catg_name');
        $this->db->join('khvt_category As cat', 'cat.id = pro.catg_id');
        $this->db->select('val.name As val_name');
        $this->db->join('khvt_unit As val', 'val.id = pro.unit_id');
        $this->db->where('pro.id', $id);
        $query = $this->db->get();
        if($query->num_rows() == 1){
            return $query->row();
        }
        return false;
    }
    function insertVT($data){
        $this->db->insert('khvt_product', $data);
        return $this->db->insert_id();
    }
    function unique_code($code){
        $query = $this->db->get_where('khvt_product', array('code' => $code));
        if($query->num_rows() > 0){
            return false;
        }
        return true;
    }
    function unique_code_edit($id,$code){
        //$query = $this->db->get_where('khvt_product', array('code' => $code));
        $this->db->select('code');
        $this->db->from('khvt_product');
        $this->db->where('id !=', $id);
        $this->db->where('code', $code);
        $query = $this->db->get();
        
        if($query->num_rows() > 0){
            return false;
        }
        return true;
    }
    function updateVT($data,$id){
        $this->db->where('id', $id);
        $this->db->update('khvt_product', $data);
    }
    
    function load_datatable_supplier($GET,$step = 1){
        
        $aColumns = array( 'sup.id', 'sup.supplier_name', 'sup.supplier_phone', 'sup.supplier_note','sup.status' );
        
        $this->db->select('sup.*');
        $this->db->from('khvt_supplier As sup');
        $this->db->where('sup.status !=',-1);
    
        /*
         * Ordering
         */
        if ( isset( $GET['iSortCol_0'] ) )
        {
            for ( $i=0 ; $i<intval( $GET['iSortingCols'] ) ; $i++ )
            {
                if ( $GET[ 'bSortable_'.intval($GET['iSortCol_'.$i]) ] == "true" )
                {
                    if( $aColumns[ $GET['iSortCol_'.$i] ] != ''){
                        $this->db->order_by($aColumns[ $GET['iSortCol_'.$i] ], $GET['sSortDir_'.$i] );
                    }
                }
            }
        }

        /* 
         * Filtering
         */
        if ( isset($GET['sSearch']) && $GET['sSearch'] != "" )
        {   
            if($aColumns[1] != ''){
                $this->db->like($aColumns[1], $GET['sSearch'] );    
                $this->db->or_like($aColumns[0], $GET['sSearch'] );    
            }
        }
    
        /* Individual column filtering */
        for ( $i=0 ; $i<count($aColumns) ; $i++ )
        {
            if ( isset($GET['bSearchable_'.$i]) && $GET['bSearchable_'.$i] == "true" && $GET['sSearch_'.$i] != '' )
            {
                if($aColumns[$i] != ''){
                    $this->db->like($aColumns[$i], $GET['sSearch_'.$i] );
                }
            }
        }
        
        $total = 0;
        $aaData = array();
        
        switch($step){
            case 1:
                /* 
                 * Paging
                 */
                if ( isset( $GET['iDisplayStart'] ) && $GET['iDisplayLength'] != '-1' ){
                    $this->db->limit($GET['iDisplayLength'], $GET['iDisplayStart']);
                }
                
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
                
                if($total > 0){
                    foreach($query->result() As $item){
                        $html = '';
                        if($item->status == -1){
                            $html = '<img src="'.base_url('template/ess/images/trash.png').'" alt="">';
                        }elseif($item->status == 1){
                            $html = '<img src="'.base_url('template/ess/images/tick.png').'" alt="">';
                        }elseif($item->status == 2){
                            $html = '<img src="'.base_url('template/ess/images/publish_x.png').'" alt="">';
                        }
                        
                        $row = array();
                        //$row[] = '<input type="radio" name="tableselected" value="">';
                        $row[] = $item->id;
                        $row[] = $item->supplier_name;
                        //$row[] = $item->supplier_phone;
                        $row[] = $item->supplier_note;
                        $row[] = $html;
                        $row['id'] = $item->id;
                        $row['modify'] = 'edit';
                        $aaData[] = $row;
                    }
                }    
            
            break;
            
            case 2:
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
            break;
            
            default:
            
            break;
        }
        
        $output = array(
            "total" => $total,
            "aaData" => $aaData
        );
        return $output;        
    }
    
    function getALLProduct(){
        $this->db->select('pro.*');
        $this->db->from('khvt_product As pro');
        $this->db->select('cat.name As catg_name');
        $this->db->join('khvt_category As cat', 'cat.id = pro.catg_id');
        $this->db->select('val.name As val_name');
        $this->db->join('khvt_unit As val', 'val.id = pro.unit_id');
        $this->db->where('pro.status !=',-1);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result();
        }
        return false;
    }
    
    function getProductBystatus($status){
        $this->db->select('pro.*');
        $this->db->from('khvt_product As pro');
        $this->db->select('cat.name As catg_name');
        $this->db->join('khvt_category As cat', 'cat.id = pro.catg_id');
        $this->db->select('val.name As val_name');
        $this->db->join('khvt_unit As val', 'val.id = pro.unit_id');
        $this->db->where('pro.status',$status);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result();
        }
        return false;
    }
    
    function getProductByCategory($catg_id){
        $this->db->select('pro.*');
        $this->db->from('khvt_product As pro');
        $this->db->select('cat.name As catg_name');
        $this->db->join('khvt_category As cat', 'cat.id = pro.catg_id');
        $this->db->select('val.name As val_name');
        $this->db->join('khvt_unit As val', 'val.id = pro.unit_id');
        $this->db->where('pro.catg_id',$catg_id);
        $this->db->where('pro.status',1);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result();
        }
        return false;
    }
    
    function getSupplierById($id){
        $this->db->select('*');
        $this->db->from('khvt_supplier');
        $this->db->where('id', $id);
        $query = $this->db->get();
        if($query->num_rows() == 1){
            return $query->row();
        }
        return false;
    }
    function getAllSupplier(){
        $this->db->select('*');
        $this->db->from('khvt_supplier');
        $this->db->where('status !=',-1);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result();
        }
        return false;
    }
    function insertSupplier($data){
        $this->db->insert('khvt_supplier', $data);
        return $this->db->insert_id();
    }
    
    function updateSupplier($data,$id){
        $this->db->where('id', $id);
        $this->db->update('khvt_supplier', $data);
    }
    function insert_import_bill($data){
        $this->db->insert('khvt_import_bill', $data);
        return $this->db->insert_id();
    }
    function insert_import_product($data){
        $this->db->insert('khvt_import_product', $data);
        return $this->db->insert_id();
    }
    
    public function unique_import_bill_code($code){
        $query = $this->db->get_where('khvt_import_bill', array('bill_code' => $code));
        if($query->num_rows() > 0){
            return false;
        }
        return true;
    }
    
    function getImportBillById($id){
        $this->db->select('bill.*');
        $this->db->from('khvt_import_bill As bill');
        $this->db->select('u.username');
        $this->db->join('sy_users As u', 'u.id = bill.created_by');
        $this->db->select('supp.supplier_name');
        $this->db->join('khvt_supplier As supp', 'supp.id = bill.supplier_id');
        $this->db->where('bill.id', $id);
        $query = $this->db->get();
        if($query->num_rows() == 1){
            return $query->row();
        }
        return false;
    }
    
    function getImportBillByDate($date){
        $this->db->select('bill.*');
        $this->db->from('khvt_import_bill As bill');
        $this->db->select('u.username');
        $this->db->join('sy_users As u', 'u.id = bill.created_by');
        $this->db->select('supp.supplier_name');
        $this->db->join('khvt_supplier As supp', 'supp.id = bill.supplier_id');
        $this->db->where('DATE(bill.created_date)', $date);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result();
        }
        return false;
    }
    
    function load_datatable_import_product($GET,$step = 1,$bill_id=0){
        
        $aColumns = array( 'pro.id', 'pro.code', 'pro.name', 'val.name', 'im.import_total', 'cat.name', 'im.note' );
        
        $this->db->select('bill.id As bill_id');
        $this->db->from('khvt_import_bill As bill');        
        $this->db->select('im.import_total, im.note');
        $this->db->join('khvt_import_product As im', 'im.bill_id = bill.id');        
        $this->db->select('pro.id,pro.code,pro.name');
        $this->db->join('khvt_product As pro', 'pro.id = im.product_id');        
        $this->db->select('cat.name As catg_name');
        $this->db->join('khvt_category As cat', 'cat.id = pro.catg_id');
        $this->db->select('val.name As val_name');
        $this->db->join('khvt_unit As val', 'val.id = pro.unit_id');        
        $this->db->where('bill.id',$bill_id);
    
        /*
         * Ordering
         */
        if ( isset( $GET['iSortCol_0'] ) )
        {
            for ( $i=0 ; $i<intval( $GET['iSortingCols'] ) ; $i++ )
            {
                if ( $GET[ 'bSortable_'.intval($GET['iSortCol_'.$i]) ] == "true" )
                {
                    if( $aColumns[ $GET['iSortCol_'.$i] ] != ''){
                        $this->db->order_by($aColumns[ $GET['iSortCol_'.$i] ], $GET['sSortDir_'.$i] );
                    }
                }
            }
        }

        /* 
         * Filtering
         */
        if ( isset($GET['sSearch']) && $GET['sSearch'] != "" )
        {   
            $this->db->like($aColumns[1], $GET['sSearch'] );    
            $this->db->or_like($aColumns[2], $GET['sSearch'] );    
        }
    
        /* Individual column filtering */
        for ( $i=0 ; $i<count($aColumns) ; $i++ )
        {
            if ( isset($GET['bSearchable_'.$i]) && $GET['bSearchable_'.$i] == "true" && $GET['sSearch_'.$i] != '' )
            {
                if($aColumns[$i] != ''){
                    $this->db->like($aColumns[$i], $GET['sSearch_'.$i] );
                }
            }
        }
        
        $total = 0;
        $aaData = array();
        
        switch($step){
            case 1:
                /* 
                 * Paging
                 */
                if ( isset( $GET['iDisplayStart'] ) && $GET['iDisplayLength'] != '-1' ){
                    $this->db->limit($GET['iDisplayLength'], $GET['iDisplayStart']);
                }
                
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
                
                if($total > 0){
                    foreach($query->result() As $item){                        
                        $row = array();
                        //$row[] = '<input type="radio" name="tableselected" value="">';
                        $row[] = $item->id;
                        $row[] = $item->code;
                        $row[] = $item->name;
                        $row[] = $item->val_name;
                        $row[] = $item->import_total;
                        $row[] = $item->catg_name;
                        $row[] = $item->note;
                        $row['id'] = $item->id;
                        $aaData[] = $row;
                    }
                }    
            
            break;
            
            case 2:
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
            break;
            
            default:
            
            break;
        }
        
        $output = array(
            "total" => $total,
            "aaData" => $aaData
        );
    
        return $output;
        
        
    }
    
    function load_datatable_filter_bill_import($GET,$step = 1){
        
        $aColumns = array( 'bill.id', 'bill.bill_code', 'u.username', 'supp.supplier_name', 'bill.created_date','bill.bill_totalvat','bill.bill_note' );
        
        $this->db->select('bill.*');
        $this->db->from('khvt_import_bill As bill');
        $this->db->select('u.username');
        $this->db->join('sy_users As u', 'u.id = bill.created_by');
        $this->db->select('supp.supplier_name');
        $this->db->join('khvt_supplier As supp', 'supp.id = bill.supplier_id');
        
        /*dieu kien filter*/
        if ( isset( $GET['filter_date_from'] ) && $GET['filter_date_from'] != '' ){
            /*$beginDate = explode("/",$begin_date);
            $beginDate_format = $beginDate[2].'-'.$beginDate[1].'-'.$beginDate[0];

            $endDate = explode("/",$end_date);
            $endDate_format = $endDate[2].'-'.$endDate[1].'-'.$endDate[0];*/
            if( isset( $GET['filter_date_to'] ) && $GET['filter_date_to'] != '' ){
                $filter_date_from = dateVnToMysql($GET['filter_date_from']);
                $filter_date_to = dateVnToMysql($GET['filter_date_to']);
                $this->db->where('bill.created_date >=', $filter_date_from.' 00:00:00');
                $this->db->where('bill.created_date <=', $filter_date_to.' 23:59:59');
            }else{
                $filter_date_from = dateVnToMysql($GET['filter_date_from']);
                $this->db->where('bill.created_date >=', $filter_date_from.' 00:00:00');
                $this->db->where('bill.created_date <=', $filter_date_from.' 23:59:59');
            }
        }
    
        /*
         * Ordering
         */
        if ( isset( $GET['iSortCol_0'] ) )
        {
            for ( $i=0 ; $i<intval( $GET['iSortingCols'] ) ; $i++ )
            {
                if ( $GET[ 'bSortable_'.intval($GET['iSortCol_'.$i]) ] == "true" )
                {
                    if( $aColumns[ $GET['iSortCol_'.$i] ] != ''){
                        $this->db->order_by($aColumns[ $GET['iSortCol_'.$i] ], $GET['sSortDir_'.$i] );
                    }
                }
            }
        }

        /* 
         * Filtering
         */
        if ( isset($GET['sSearch']) && $GET['sSearch'] != "" )
        {   
            $this->db->like($aColumns[1], $GET['sSearch'] );    
            $this->db->or_like($aColumns[2], $GET['sSearch'] );    
        }
    
        /* Individual column filtering */
        for ( $i=0 ; $i<count($aColumns) ; $i++ )
        {
            if ( isset($GET['bSearchable_'.$i]) && $GET['bSearchable_'.$i] == "true" && $GET['sSearch_'.$i] != '' )
            {
                if($aColumns[$i] != ''){
                    $this->db->like($aColumns[$i], $GET['sSearch_'.$i] );
                }
            }
        }
        
        $total = 0;
        $aaData = array();
        
        switch($step){
            case 1:
                /* 
                 * Paging
                 */
                if ( isset( $GET['iDisplayStart'] ) && $GET['iDisplayLength'] != '-1' ){
                    $this->db->limit($GET['iDisplayLength'], $GET['iDisplayStart']);
                }
                
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
                
                if($total > 0){
                    foreach($query->result() As $item){                        
                        $row = array();
                        //$row[] = '<input type="radio" name="tableselected" value="">';
                        $row[] = $item->id;
                        $row[] = '<a href="'.site_url('nk/bill/'.$item->id).'">'.$item->bill_code.'</a>';
                        $row[] = $item->username;
                        $row[] = $item->supplier_name;
                        $row[] = dateMysqlToVN($item->created_date);
                        $row[] = decimal_format($item->bill_totalvat);
                        $row[] = $item->bill_note;
                        $row['id'] = $item->id;
                        $aaData[] = $row;
                    }
                }    
            
            break;
            
            case 2:
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
            break;
            
            default:
            
            break;
        }
        
        $output = array(
            "total" => $total,
            "aaData" => $aaData
        );
    
        return $output;
        
        
    }
    
    function getEmployee_recipient(){
        $this->db->select('*');
        $this->db->from('sy_employee');
        //$this->db->where('status !=',-1);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result();
        }
        return false;
    }
    
    function insert_export_bill($data){
        $this->db->insert('khvt_export_bill', $data);
        return $this->db->insert_id();
    }
    function updateExportBill($data,$id){
        $this->db->where('id', $id);
        $this->db->update('khvt_export_bill', $data);
    }
    function insert_export_product($data){
        $this->db->insert('khvt_export_product', $data);
        return $this->db->insert_id();
    }
    function updateExportProduct($data,$id){
        $this->db->where('id', $id);
        $this->db->update('khvt_export_product', $data);
    }
    public function unique_export_bill_code($code){
        $query = $this->db->get_where('khvt_export_bill', array('bill_code' => $code));
        if($query->num_rows() > 0){
            return false;
        }
        return true;
    }
    function getExportBillById($id){
        $this->db->select('bill.*');
        $this->db->from('khvt_export_bill As bill');
        $this->db->select('u.username');
        $this->db->join('sy_users As u', 'u.id = bill.created_by');
        $this->db->select('emp.name');
        $this->db->join('sy_employee As emp', 'emp.id = bill.recipient');
        $this->db->where('bill.id', $id);
        $query = $this->db->get();
        if($query->num_rows() == 1){
            return $query->row();
        }
        return false;
    }
    function getExportBillByDate($date){
        $this->db->select('bill.*');
        $this->db->from('khvt_export_bill As bill');
        $this->db->select('u.username');
        $this->db->join('sy_users As u', 'u.id = bill.created_by');
        $this->db->select('emp.name');
        $this->db->join('sy_employee As emp', 'emp.id = bill.recipient');
        $this->db->where('DATE(bill.created_date)', $date);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result();
        }
        return false;
    }
    function countExportBillByDate($date){
        $this->db->select('COUNT(bill.id) As count');
        $this->db->from('khvt_export_bill As bill');
        $this->db->select('u.username');
        $this->db->join('sy_users As u', 'u.id = bill.created_by');
        $this->db->select('emp.name');
        $this->db->join('sy_employee As emp', 'emp.id = bill.recipient');
        $this->db->where('DATE(bill.created_date)', $date);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            $resulft = $query->row();
            return $resulft->count;
        }
        return false;
    }
    function getExportBillByConstruction($cons_id){
        $this->db->select('bill.*');
        $this->db->from('khvt_export_bill As bill');
        $this->db->select('u.username');
        $this->db->join('sy_users As u', 'u.id = bill.created_by');
        $this->db->select('emp.name');
        $this->db->join('sy_employee As emp', 'emp.id = bill.recipient');
        $this->db->where('bill.cons_id', $cons_id);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result();
        }
        return false;
    }
    
    function load_datatable_export_product($GET,$step = 1,$bill_id=0){
        
        $aColumns = array( 'pro.id', 'pro.code', 'pro.name', 'val.name', 'im.import_total', 'cat.name', 'im.note' );
        
        $this->db->select('bill.id As bill_id');
        $this->db->from('khvt_export_bill As bill');        
        $this->db->select('ex.id As export_id,ex.export_total, ex.revert_total, ex.export_price, ex.note');
        $this->db->join('khvt_export_product As ex', 'ex.bill_id = bill.id');        
        $this->db->select('pro.id,pro.code,pro.name');
        $this->db->join('khvt_product As pro', 'pro.id = ex.product_id');        
        $this->db->select('cat.name As catg_name');
        $this->db->join('khvt_category As cat', 'cat.id = pro.catg_id');
        $this->db->select('val.name As val_name');
        $this->db->join('khvt_unit As val', 'val.id = pro.unit_id');        
        $this->db->where('bill.id',$bill_id);
    
        /*
         * Ordering
         */
        if ( isset( $GET['iSortCol_0'] ) )
        {
            for ( $i=0 ; $i<intval( $GET['iSortingCols'] ) ; $i++ )
            {
                if ( $GET[ 'bSortable_'.intval($GET['iSortCol_'.$i]) ] == "true" )
                {
                    if( $aColumns[ $GET['iSortCol_'.$i] ] != ''){
                        $this->db->order_by($aColumns[ $GET['iSortCol_'.$i] ], $GET['sSortDir_'.$i] );
                    }
                }
            }
        }

        /* 
         * Filtering
         */
        if ( isset($GET['sSearch']) && $GET['sSearch'] != "" )
        {   
            $this->db->like($aColumns[1], $GET['sSearch'] );    
            $this->db->or_like($aColumns[2], $GET['sSearch'] );    
        }
    
        /* Individual column filtering */
        for ( $i=0 ; $i<count($aColumns) ; $i++ )
        {
            if ( isset($GET['bSearchable_'.$i]) && $GET['bSearchable_'.$i] == "true" && $GET['sSearch_'.$i] != '' )
            {
                if($aColumns[$i] != ''){
                    $this->db->like($aColumns[$i], $GET['sSearch_'.$i] );
                }
            }
        }
        
        $total = 0;
        $aaData = array();
        
        switch($step){
            case 1:
                /* 
                 * Paging
                 */
                if ( isset( $GET['iDisplayStart'] ) && $GET['iDisplayLength'] != '-1' ){
                    $this->db->limit($GET['iDisplayLength'], $GET['iDisplayStart']);
                }
                
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
                
                if($total > 0){
                    foreach($query->result() As $item){                        
                        $row = array();
                        //$row[] = '<input type="radio" name="tableselected" value="">';
                        $row[] = $item->id;
                        $row[] = $item->code;
                        $row[] = $item->name;
                        $row[] = $item->val_name;
                        $row[] = decimal_format($item->export_total);
                        $row[] = decimal_format($item->export_price);
                        $row[] = decimal_format($item->export_total*$item->export_price);
                        $row[] = $item->note;
                        $row[] = decimal_format($item->revert_total);
                        $row['id'] = $item->export_id;
                        $row['revert'] = ($item->revert_total >= $item->export_total )?'no':'yes';
                        $aaData[] = $row;
                    }
                }    
            
            break;
            
            case 2:
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
            break;
            
            default:
            
            break;
        }
        
        $output = array(
            "total" => $total,
            "aaData" => $aaData
        );
    
        return $output;    
    }
    
    function load_datatable_filter_bill_export($GET,$step = 1){
        
        $aColumns = array( 'bill.id', 'bill.bill_code', 'u.username', 'em.name', 'bill.created_date','bill.bill_totalvat','bill.bill_note' );
        
        $this->db->select('bill.*');
        $this->db->from('khvt_export_bill As bill');
        $this->db->select('u.username');
        $this->db->join('sy_users As u', 'u.id = bill.created_by');
        $this->db->select('em.name As employee_name');
        $this->db->join('sy_employee As em', 'em.id = bill.recipient');
        
        /*dieu kien filter*/
        if ( isset( $GET['filter_bill_code'] ) && $GET['filter_bill_code'] != '' ){
            $this->db->where('bill.bill_code', $GET['filter_bill_code']);
        }else{
            if ( isset( $GET['filter_date_from'] ) && $GET['filter_date_from'] != '' ){
                /*$beginDate = explode("/",$begin_date);
                $beginDate_format = $beginDate[2].'-'.$beginDate[1].'-'.$beginDate[0];

                $endDate = explode("/",$end_date);
                $endDate_format = $endDate[2].'-'.$endDate[1].'-'.$endDate[0];*/
                if( isset( $GET['filter_date_to'] ) && $GET['filter_date_to'] != '' ){
                    $filter_date_from = dateVnToMysql($GET['filter_date_from']);
                    $filter_date_to = dateVnToMysql($GET['filter_date_to']);
                    $this->db->where('bill.created_date >=', $filter_date_from.' 00:00:00');
                    $this->db->where('bill.created_date <=', $filter_date_to.' 23:59:59');
                }else{
                    $filter_date_from = dateVnToMysql($GET['filter_date_from']);
                    $this->db->where('bill.created_date >=', $filter_date_from.' 00:00:00');
                    $this->db->where('bill.created_date <=', $filter_date_from.' 23:59:59');
                }
            }
        }
        
        /*
         * Ordering
         */
        if ( isset( $GET['iSortCol_0'] ) )
        {
            for ( $i=0 ; $i<intval( $GET['iSortingCols'] ) ; $i++ )
            {
                if ( $GET[ 'bSortable_'.intval($GET['iSortCol_'.$i]) ] == "true" )
                {
                    if( $aColumns[ $GET['iSortCol_'.$i] ] != ''){
                        $this->db->order_by($aColumns[ $GET['iSortCol_'.$i] ], $GET['sSortDir_'.$i] );
                    }
                }
            }
        }

        /* 
         * Filtering
         */
        if ( isset($GET['sSearch']) && $GET['sSearch'] != "" )
        {   
            $this->db->like($aColumns[1], $GET['sSearch'] );    
            $this->db->or_like($aColumns[2], $GET['sSearch'] );    
        }
    
        /* Individual column filtering */
        for ( $i=0 ; $i<count($aColumns) ; $i++ )
        {
            if ( isset($GET['bSearchable_'.$i]) && $GET['bSearchable_'.$i] == "true" && $GET['sSearch_'.$i] != '' )
            {
                if($aColumns[$i] != ''){
                    $this->db->like($aColumns[$i], $GET['sSearch_'.$i] );
                }
            }
        }
        
        $total = 0;
        $aaData = array();
        
        switch($step){
            case 1:
                /* 
                 * Paging
                 */
                if ( isset( $GET['iDisplayStart'] ) && $GET['iDisplayLength'] != '-1' ){
                    $this->db->limit($GET['iDisplayLength'], $GET['iDisplayStart']);
                }
                
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
                
                if($total > 0){
                    foreach($query->result() As $item){                        
                        $row = array();
                        //$row[] = '<input type="radio" name="tableselected" value="">';
                        $row[] = $item->id;
                        $row[] = '<a href="'.site_url('xk/bill/'.$item->id).'">'.$item->bill_code.'</a>';
                        $row[] = $item->username;
                        $row[] = $item->employee_name;
                        $row[] = dateMysqlToVN($item->created_date);
                        $row[] = decimal_format($item->bill_totalvat);
                        $row[] = $item->bill_note;
                        $row['id'] = $item->id;
                        $aaData[] = $row;
                    }
                }    
            
            break;
            
            case 2:
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
            break;
            
            default:
            
            break;
        }
        
        $output = array(
            "total" => $total,
            "aaData" => $aaData
        );
    
        return $output;
        
        
    }
    
    function getExportProductById($id){
        $this->db->select('ex.*');
        $this->db->from('khvt_export_product As ex');
        $this->db->select('pro.code,pro.name');
        $this->db->join('khvt_product As pro', 'pro.id = ex.product_id');        
        $this->db->where('ex.id',$id);
        
        $query = $this->db->get();
        if($query->num_rows() == 1){
            return $query->row();
        }
        return false;
    }
    
    function insert_revert_product($data){
        $this->db->insert('khvt_revert_product', $data);
        return $this->db->insert_id();
    }
    
    function load_datatable_revert_product($GET,$step = 1,$bill_id=0){
        
        $aColumns = array( 'pro.id', 'pro.code', 'pro.name', 'val.name', 'im.import_total', 'cat.name', 'im.note' );
        
        $this->db->select('bill.id As bill_id');
        $this->db->from('khvt_export_bill As bill');        
        $this->db->select('re.id As revert_id,re.revert_total, re.revert_date');
        $this->db->join('khvt_revert_product As re', 're.bill_id = bill.id');        
        $this->db->select('pro.id,pro.code,pro.name');
        $this->db->join('khvt_product As pro', 'pro.id = re.product_id');        
        $this->db->select('cat.name As catg_name');
        $this->db->join('khvt_category As cat', 'cat.id = pro.catg_id');
        $this->db->select('val.name As val_name');
        $this->db->join('khvt_unit As val', 'val.id = pro.unit_id');        
        $this->db->where('bill.id',$bill_id);
    
        /*
         * Ordering
         */
        if ( isset( $GET['iSortCol_0'] ) )
        {
            for ( $i=0 ; $i<intval( $GET['iSortingCols'] ) ; $i++ )
            {
                if ( $GET[ 'bSortable_'.intval($GET['iSortCol_'.$i]) ] == "true" )
                {
                    if( $aColumns[ $GET['iSortCol_'.$i] ] != ''){
                        $this->db->order_by($aColumns[ $GET['iSortCol_'.$i] ], $GET['sSortDir_'.$i] );
                    }
                }
            }
        }

        /* 
         * Filtering
         */
        if ( isset($GET['sSearch']) && $GET['sSearch'] != "" )
        {   
            $this->db->like($aColumns[1], $GET['sSearch'] );    
            $this->db->or_like($aColumns[2], $GET['sSearch'] );    
        }
    
        /* Individual column filtering */
        for ( $i=0 ; $i<count($aColumns) ; $i++ )
        {
            if ( isset($GET['bSearchable_'.$i]) && $GET['bSearchable_'.$i] == "true" && $GET['sSearch_'.$i] != '' )
            {
                if($aColumns[$i] != ''){
                    $this->db->like($aColumns[$i], $GET['sSearch_'.$i] );
                }
            }
        }
        
        $total = 0;
        $aaData = array();
        
        switch($step){
            case 1:
                /* 
                 * Paging
                 */
                if ( isset( $GET['iDisplayStart'] ) && $GET['iDisplayLength'] != '-1' ){
                    $this->db->limit($GET['iDisplayLength'], $GET['iDisplayStart']);
                }
                
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
                
                if($total > 0){
                    foreach($query->result() As $item){                        
                        $row = array();
                        //$row[] = '<input type="radio" name="tableselected" value="">';
                        $row[] = $item->revert_id;
                        $row[] = $item->code;
                        $row[] = $item->name;
                        $row[] = $item->val_name;
                        $row[] = decimal_format($item->revert_total);
                        $row[] = dateMysqlToVN($item->revert_date,'full');
                        $row['id'] = $item->revert_id;
                        $aaData[] = $row;
                    }
                }    
            
            break;
            
            case 2:
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
            break;
            
            default:
            
            break;
        }
        
        $output = array(
            "total" => $total,
            "aaData" => $aaData
        );
    
        return $output;    
    }
    
    function unique_report_code($code){
        $query = $this->db->get_where('khvt_report', array('code' => $code));
        if($query->num_rows() > 0){
            return false;
        }
        return true;
    }
    
    function getReportByCode($code){
        $this->db->select('*');
        $this->db->from('khvt_report');
        $this->db->where('code', $code);
        $query = $this->db->get();
        if($query->num_rows() == 1){
            return $query->row();
        }
        return false;
    }
    
    function getFilterReport($date_from = '',$date_to = ''){
        $this->db->select('*');
        $this->db->from('khvt_report');
        if( $date_from != '' && $date_to != ''){
            $date_from = dateVnToMysql($date_from);
            $date_to = dateVnToMysql($date_to);
            $this->db->where('DATE(created_date) >=', $date_from);
            $this->db->where('DATE(created_date) <=', $date_to);
        }else{
            $date_from = dateVnToMysql($date_from);
            $this->db->where('DATE(created_date)', $date_from);
        }
        $this->db->order_by("created_date", "desc"); 
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result();
        }
        return false;
    }
    
    function insertReport($data){
        $this->db->insert('khvt_report', $data);
        return $this->db->insert_id();
    }
    function updateReport($data,$id){
        $this->db->where('id', $id);
        $this->db->update('khvt_report', $data);
    }
    
    function getALLProductImportByDate($date){
        $this->db->select('im.*');
        $this->db->from('khvt_import_product As im');
        $this->db->join('khvt_import_bill As bill', 'bill.id = im.bill_id');
        
        $this->db->where('DATE(bill.created_date)', $date);
        //$this->db->where('bill.created_date <=', $GET['filter_date_to'].' 23:59:59');
        $query = $this->db->get();
        $data = array();
        if($query->num_rows() > 0){
            $result = $query->result();
            foreach($result As $row){
                if(isset($data[$row->product_id])){
                    $data[$row->product_id]['count'] += $row->import_total;
                }else{
                    $data[$row->product_id] = array(
                        'product_id' => $row->product_id,
                        'count' => $row->import_total
                    );
                }
            }
        }
        return $data;
    }
    function getALLProductExportByDate($date){
        $this->db->select('ex.*');
        $this->db->from('khvt_export_product As ex');
        $this->db->join('khvt_export_bill As bill', 'bill.id = ex.bill_id');
        
        $this->db->where('DATE(bill.created_date)', $date);
        //$this->db->where('bill.created_date <=', $GET['filter_date_to'].' 23:59:59');
        $query = $this->db->get();
        $data = array();
        if($query->num_rows() > 0){
            $result = $query->result();
            foreach($result As $row){
                if(isset($data[$row->product_id])){
                    $data[$row->product_id]['count'] += $row->export_total;
                }else{
                    $data[$row->product_id] = array(
                        'product_id' => $row->product_id,
                        'count' => (int)$row->export_total
                    );
                }
            }
        }
        return $data;
    }
    function getALLProductRevertByDate($date){
        $this->db->select('re.*');
        $this->db->from('khvt_revert_product As re');
        $this->db->join('khvt_export_bill As bill', 'bill.id = re.bill_id');
        $this->db->select('pro.code, pro.price');
        $this->db->join('khvt_product As pro', 'pro.id = re.product_id');
        
        $this->db->where('DATE(re.revert_date)', $date);
        //$this->db->where('bill.created_date <=', $GET['filter_date_to'].' 23:59:59');
        $query = $this->db->get();
        $data = array();
        if($query->num_rows() > 0){
            $result = $query->result();
            foreach($result As $row){
                if(isset($data[$row->product_id])){
                    $data[$row->product_id]['count'] += $row->revert_total;
                }else{
                    $data[$row->product_id] = array(
                        'product_id' => $row->product_id,
                        'count' => (int)$row->revert_total,
                        'code' => $row->code,
                        'price' => $row->price
                    );
                }
            }
        }
        return $data;
    }
    
    function getALLProductToReport(){
        $this->db->select('pro.*');
        $this->db->from('khvt_product As pro');
        $this->db->select('cat.name As catg_name');
        $this->db->join('khvt_category As cat', 'cat.id = pro.catg_id');
        $this->db->select('val.name As val_name');
        $this->db->join('khvt_unit As val', 'val.id = pro.unit_id');
        $this->db->where('pro.status !=',-1);
        $query = $this->db->get();
        $product_data = array();
        
        if($query->num_rows() > 0){
            $products = $query->result();
            
            foreach($products As $product){
                $product_data[$product->id] = array(
                    'c' => $product->code,
                    'ft' => decimal_format($product->total),
                    'it' => 0,
                    'et' => 0,
                    'rt' => 0,
                    'p' => decimal_format($product->price)
                );
            }
            
        }
        return $product_data;
    }
    
    function getALLProductData(){
        $this->db->select('pro.*');
        $this->db->from('khvt_product As pro');
        $this->db->select('cat.name As catg_name');
        $this->db->join('khvt_category As cat', 'cat.id = pro.catg_id');
        $this->db->select('val.name As val_name');
        $this->db->join('khvt_unit As val', 'val.id = pro.unit_id');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result();
        }
        return false;
    }
    
    function load_datatable_construction($GET,$step = 1){
        
        $aColumns = array( 'cons.id', 'cons.name', 'cons.created_date', 'cons.end_date', 'cons.status' );
        
        $this->db->select('cons.*');
        $this->db->from('khvt_construction As cons');
        $this->db->where('cons.status !=',-1);
    
        /*
         * Ordering
         */
        if ( isset( $GET['iSortCol_0'] ) )
        {
            for ( $i=0 ; $i<intval( $GET['iSortingCols'] ) ; $i++ )
            {
                if ( $GET[ 'bSortable_'.intval($GET['iSortCol_'.$i]) ] == "true" )
                {
                    if( $aColumns[ $GET['iSortCol_'.$i] ] != ''){
                        $this->db->order_by($aColumns[ $GET['iSortCol_'.$i] ], $GET['sSortDir_'.$i] );
                    }
                }
            }
        }

        /* 
         * Filtering
         */
        if ( isset($GET['sSearch']) && $GET['sSearch'] != "" )
        {   
            if($aColumns[1] != ''){
                $this->db->like($aColumns[1], $GET['sSearch'] );    
                //$this->db->or_like($aColumns[0], $GET['sSearch'] );    
            }
        }
    
        /* Individual column filtering */
        for ( $i=0 ; $i<count($aColumns) ; $i++ )
        {
            if ( isset($GET['bSearchable_'.$i]) && $GET['bSearchable_'.$i] == "true" && $GET['sSearch_'.$i] != '' )
            {
                if($aColumns[$i] != ''){
                    $this->db->like($aColumns[$i], $GET['sSearch_'.$i] );
                }
            }
        }
        
        $total = 0;
        $aaData = array();
        
        switch($step){
            case 1:
                /* 
                 * Paging
                 */
                if ( isset( $GET['iDisplayStart'] ) && $GET['iDisplayLength'] != '-1' ){
                    $this->db->limit($GET['iDisplayLength'], $GET['iDisplayStart']);
                }
                
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
                
                if($total > 0){
                    foreach($query->result() As $item){
                        $html = '';
                        if($item->status == -1){
                            $html = '<img src="'.base_url('template/ess/images/trash.png').'" alt="delete">';
                        }elseif($item->status == 1){
                            $html = '<img src="'.base_url('template/ess/images/tick.png').'" alt="Dang thi cong">'.lang('vt_tt_dangthicong');
                        }elseif($item->status == 2){
                            $html = '<img src="'.base_url('template/ess/images/publish_x.png').'" alt="Ket thuc">'.lang('vt_tt_ketthuc');
                        }
                        
                        $row = array();
                        //$row[] = '<input type="radio" name="tableselected" value="">';
                        $row[] = $item->id;
                        $row[] = '<a href="'.site_url('vt/construction_view/'.$item->id).'">'.$item->name.'</a>';
                        $row[] = dateMysqlToVN($item->created_date);
                        $row[] = dateMysqlToVN($item->end_date);
                        $row[] = $html;
                        $row['id'] = $item->id;
                        $row['modify'] = 'edit';
                        $aaData[] = $row;
                    }
                }    
            
            break;
            
            case 2:
                $query = $this->db->get();
                if($query->num_rows() > 0){
                    $total = $query->num_rows();
                }
            break;
            
            default:
            
            break;
        }
        
        $output = array(
            "total" => $total,
            "aaData" => $aaData
        );
        return $output;        
    }
    
    function getConstructionById($id){
        $this->db->select('*');
        $this->db->from('khvt_construction');
        $this->db->where('id', $id);
        $query = $this->db->get();
        if($query->num_rows() == 1){
            return $query->row();
        }
        return false;
    }
    
    function insertConstruction($data){
        $this->db->insert('khvt_construction', $data);
        return $this->db->insert_id();
    }
    
    function updateConstruction($data,$id){
        $this->db->where('id', $id);
        $this->db->update('khvt_construction', $data);
    }
    
    function getConstructionByStatus($status){
        $query = $this->db->get('khvt_construction');
        $this->db->where('status', $status);
        if($query->num_rows() > 0){
            return $query->result();
        }
        return false;
    }
    
    function getProductExportByConstruction($cons_id){
        $this->db->select('pro.id, pro.code, pro.name');
        $this->db->from('khvt_product As pro');
        $this->db->select('val.name As dvt');
        $this->db->join('khvt_unit As val', 'val.id = pro.unit_id');
        $this->db->select('ex.export_total, ex.revert_total');
        $this->db->join('khvt_export_product As ex', 'ex.product_id = pro.id');
        $this->db->join('khvt_export_bill As bill', 'bill.id = ex.bill_id');
        $this->db->join('khvt_construction As cons', 'cons.id = bill.cons_id');
        $this->db->where('cons.id', $cons_id);
        //$this->db->where('bill.created_date <=', $GET['filter_date_to'].' 23:59:59');
        $query = $this->db->get();
        $data = array();
        if($query->num_rows() > 0){
            $result = $query->result();
            foreach($result As $row){
                if(isset($data[$row->id])){
                    $data[$row->id]['export'] += $row->export_total;
                    $data[$row->id]['revert'] += $row->revert_total;
                }else{
                    $data[$row->id] = array(
                        'id' => $row->id,
                        'code' => $row->code,
                        'name' => $row->name,
                        'dvt' => $row->dvt,
                        'export' => $row->export_total,
                        'revert' => $row->revert_total
                    );
                }
            }
        }
        return $data;
    }
    
    function arraynumeric_check($arr_nummber){
        if( count($arr_nummber) > 0 ){
            foreach($arr_nummber As $number){
                $isnumber = (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $number);
                if( !$isnumber ){
                    return false;
                }
            }
        }else{
            return false;
        }
        return true;
    }
    
}
?>
